﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.Core.Attach;
using Jzy.Core.Utility;
using Jzy.Core.Common.Basic;
using Jzy.BLL.SiteAdmin;

namespace Jzy.Web.SiteAdmin.Picture
{
    public partial class GroupPictureList : PageList
    {
        private string ArticleID
        {
            get
            {
                return Request["ArticleID"];
            }
        }


        private ImageBO _image;
        private ImageBO ArticleImage
        {
            get
            {
                if (_image == null)
                {
                    _image = new ImageBO(Jzy.Common.TableName.CmsArticlePicture);
                }
                return _image;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindView();
                lblArticleName.Text = (new ArticleBO()).GetName(ArticleID);             
            }
            AddAttr();
            base.InitSelectID(hidSelectIDs);
        }

        private void AddAttr()
        {
            JSUtil.ShowForm(lnkAdd, "上传图片", null, GlobalVar.WebCurrPath + "GroupPictureEdit.aspx?ArticleID=" + ArticleID, 380, 180);
            JSUtil.ShowForm(lnkBatchAdd, "批量上传图片", null, GlobalVar.WebCurrPath + "GroupPictureBatchAdd.aspx?ArticleID=" + ArticleID, 400, 205);

            JSUtil.DelConfirm(btnDelete, "图片");
            JSUtil.ClickBtn(lnkDelete, btnDelete);
        }

        protected string GetFileName(object name)
        {
            return name.ToString().Replace(".jpg", "");
        }

        private void BindView()
        {
            List<ImageInfo> list = ArticleImage.GetList(int.Parse(ArticleID), null);
            lblStatistic.Text = "[&nbsp;共<b style='color:red;'> " + list.Count() + "</b>&nbsp;张图片&nbsp;]";
            lv.DataKeyNames = new string[] { "ID" };
            lv.DataSource = list;
            lv.DataBind();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (strSelectIDs == "")
            {
                return;
            }

            ImageBO img = new ImageBO(Jzy.Common.TableName.CmsArticlePicture);


            List<int> pictureIDs = StrUtil.SplitToInt(strSelectIDs);

            img.Delete(pictureIDs);

            BindView();
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindView();
        }

        protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                CheckBox chkSelect = (CheckBox)e.Item.FindControl("chkSelect");
                string strID = lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["ID"].ToString();

                chkSelect.Attributes.Add("onclick", "chk_click(this,'" + strID + "')");
            }
        }

    }
}
